package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.bean.DeviceAttributeBean;
import com.hzdesk.media.koreabatchmatvector.config.RBConfig;
import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;
import com.hzdesk.media.koreabatchmatvector.inter.IDownloadRequestListenter;
import com.hzdesk.media.koreabatchmatvector.inter.IModelCalBack;
import com.hzdesk.media.koreabatchmatvector.inter.NetworkRequestListener;
import com.hzdesk.media.koreabatchmatvector.utils.DeleteDirTest;
import com.hzdesk.media.koreabatchmatvector.utils.MacUtil;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/6/26.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class NetworkRequest {

    private NetworkRequestListener mNetworkRequestListener = null;
    private IDownloadRequestListenter mIDownloadRequestListenter = null;
    public NetworkRequest( ){}
    public void setmIDownloadRequestListenter(IDownloadRequestListenter mIDownloadRequestListenter) {
        this.mIDownloadRequestListenter = mIDownloadRequestListenter;
    }

    public void setmNetworkRequestListener(NetworkRequestListener mNetworkRequestListener) {
        this.mNetworkRequestListener = mNetworkRequestListener;
        //requestServerDate(context, Url);
    }

    public void requestServerDate(Context context, String url) {
        OkHttpUtils
                .get()
                .url(url)
                .addHeader("sessionId", MacUtil.getMacAddress(context).toString().trim())
                .addHeader("sourceType", "1")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        if (mNetworkRequestListener != null) {
                            mNetworkRequestListener.success(response);
                        }else{
                            Logger.d("parseNetworkResponse  初始化可立批设备 %s","requestServerDate");
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (mNetworkRequestListener != null) {
                            mNetworkRequestListener.fail("请查看网络 1");
                        }else{
                            Logger.d("parseNetworkResponse  初始化可立批设备 %s","onError");
                        }
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    public void DownLoadFile(   String Path,String fileName,String url){
        OkHttpUtils//
                .get()//
                .url(url)//
                .build()//
                .execute(new FileCallBack(Path, fileName) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                            if(mIDownloadRequestListenter != null){
                                mIDownloadRequestListenter.fail(e.getMessage());
                            }
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        if(mIDownloadRequestListenter != null){
                            mIDownloadRequestListenter.success(response,id);
                        }
                    }

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        if(mIDownloadRequestListenter != null){
                            mIDownloadRequestListenter.Progress(progress);
                        }
                    }
                });
    }


}
